﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

using KPIReport.Library.DomainObject.Administration.UserManagement;
using KPIReport.Library.DataAccess.Administration.UserManagement;
//using KPIReport.Library.DataAccess.Administration.Catalog;
//using SIPO.Library.DataAccess.Transaction;
//using SIPO.Library.DomainObject.Transaction;
using KPIReport.Web.Support;

using System.Configuration;
using System.Text;

namespace KPIReport.Web
{

public partial class Site : BaseMasterPage
    {

        private recMenuRepo menuDal = null;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!Page.IsPostBack)
            {
                Page.Header.DataBind();
                LinkLogout.HRef = Support.Helper.GetAppVirtualPath(this.Page) + "/forms/authentication/logout.aspx";
                ltrUserName.Text = "Logged in as "+base.UserLoggedIn;

                menuDal = new recMenuRepo(ConfigurationManager.ConnectionStrings["KPIReportConn"].ConnectionString);

               LoadMenu(base.UserID);
               // LoadSiteMap(base.MenuID);
            }

        }

        private void LoadMenu(int UserID)
        {
            

            List<int> roleIds = new List<int>();

            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["KPIReportConn"].ConnectionString);
            membership.GetRoles(UserID).ForEach(delegate(recUserRole role)
            {
                roleIds.Add(role.RoleID.Value);
            });

            List<recMenu> menus = membership.GetMenusByRoleIDs(roleIds);

            mnMaster.Items.Clear(); // Clear Menu Items
            AddItems(mnMaster.Items, null, menus); // set Null for the first time to load BaseParent.

        }

        private void LoadSiteMap(int menuId)
        {
            //var currentMenu = menuDal.GetHierarchyList("WHERE MenuID = " + menuId);

            //StringBuilder sbSitemap = new StringBuilder();

            //sbSitemap.AppendFormat("<li><a   href=\"{0}\" title=\"Home\">", Support.Helper.GetAppVirtualPath(this.Page) + "/default.aspx");
            //sbSitemap.AppendFormat("<img src=\"{0}\" alt=\"Home\"  />", ResolveUrl("images/breadcrumb/home.gif"));
            //sbSitemap.Append("</a></li>");

            //Stack<recMenu> siteMenus = new Stack<recMenu>();

            //if (currentMenu.Count > 0)
            //{
            //    var count = Convert.ToInt32(currentMenu[0].Level) + 1;

            //    var currentId = currentMenu[0].MenuID;

            //    for (var i = 0; i < count; i++)
            //    {
            //        recMenu dto = menuDal.GetById(currentId);
            //        if (dto.MenuID > 0)
            //        {
            //            siteMenus.Push(dto);
            //            currentId = (dto.ParentMenuID.HasValue) ? dto.ParentMenuID.Value : 0;
            //        }
            //    }

            //    foreach (recMenu dto in siteMenus)
            //    {
            //        var navigateUrl = (string.IsNullOrEmpty(dto.URL) ? "#" : dto.URL + (dto.URL.Contains('?') ? "&menuid=" + dto.MenuID : "?menuid=" + dto.MenuID));
                    
            //         sbSitemap.AppendFormat("<li><a  href=\"{0}\" title=\"{1}\">{1}</a></li>", navigateUrl, dto.MenuName);
            //    }

            //}

           // ltrSiteMap.Text = sbSitemap.ToString();

        }

        private void AddItems(MenuItemCollection items, decimal? id, List<recMenu> menus)
        {
           
            var getParents = menus.Where(x => x.ParentMenuID == id).OrderBy(x => x.OrderSeq);

            foreach (recMenu menu in getParents)
            {
                MenuItem item = new MenuItem()
                {
                    Text = menu.MenuName,
                    NavigateUrl = (string.IsNullOrEmpty(menu.URL) ? "#" : menu.URL + (menu.URL.Contains('?') ? "&menuid=" + menu.MenuID : "?menuid=" + menu.MenuID))
                };
                

                this.AddItems(item.ChildItems, menu.MenuID, menus);
                items.Add(item);
              
            }
        }

    }
}
